﻿/////////////////////////////////////////////////////////////////////////////////
//
// InstantCube
// - Automatically generates Analysis Services cubes
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on InstantCube website: http://instantcube.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace AdvantIQ.InstantCube.Model
{
    public class Cube
    {
        public string ID { get; set; }
        public CubeMeasureGroup[] MeasureGroups { get; set; }
        public CalculatedMember[] CalculatedMembers { get; set; }
        public CalculatedCell[] CalculatedCells { get; set; }
        public CubeAction[] CubeActions { get; set; }
        public KPI[] KPIs { get; set; }

        private Project project;

        public Cube(XElement srcNode, Project project)
        {
            try
            {
                this.project = project;

                ID = srcNode.Attribute("id").Value;
                var lst = new List<CubeMeasureGroup>();
                foreach (var node in srcNode.Element("measureGroups").Elements("measureGroup"))
                    lst.Add(new CubeMeasureGroup(node, project));

                MeasureGroups = lst.ToArray();

                CalculatedMembers = srcNode.Element("calculations").Elements("calculatedMember").Select(n => new CalculatedMember(n)).ToArray();
                CalculatedCells = srcNode.Element("calculations").Elements("calculatedCell").Select(n => new CalculatedCell(n)).ToArray();
                CubeActions = srcNode.Element("actions").Elements("cubeAction").Select(n => new CubeAction(n)).ToArray();
                KPIs = srcNode.Element("kpis").Elements("kpi").Select(n => new KPI(n)).ToArray();
            }
            catch (ModelErrorException ex)
            {
                throw new ModelErrorException(this, ex);
            }
            catch (Exception ex)
            {
                throw new ModelErrorException(this, ex.Message);
            }
        }

        public override string ToString()
        {
            if (ID != null)
                return string.Format("Cube({0})", ID);
            else
                return "Cube";
        }
    }
}
